package com.broad.commerce.code.base;

import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.code.modules.jdbc.MapperModule;

import java.util.List;

/**
 * The type Abstract xml service.
 * @author stephen
 * @version V1.0
 * @date 2022 -02-07 16:50
 * @description: AbstractXmlService
 */
public abstract class AbstractXmlService extends AbstractBaseService {
	@Override
	public String createXmlCode(BaseModule baseModule) {
		StringBuilder sb = new StringBuilder();
		//xml 头部
		this.createXmlHeaderCode(sb);
		//xml 命名空间tag
		this.createXmlTagCode(sb, (MapperModule) baseModule);
		//xml 内容 sql
		this.createXmlContent(sb, (MapperModule) baseModule);
		//xml 结束标签tag
		this.createXmlEndTagCode(sb);
		return sb.toString();
	}

	/**
	 * Create class file.
	 * @param baseModule the base module
	 * @param rootPath the root path
	 * @param fileType the file type
	 */
	@Override
	public void createClassFile(BaseModule baseModule, String filePath, String fileType) {
		super.createClassFile(baseModule, filePath, fileType);
	}

	/**
	 * Create batch class files.
	 * @param baseModules the base modules
	 * @param rootPath the root path
	 * @param fileType
	 */
	@Override
	public void batchCreateClassFiles(List<BaseModule> baseModules, String filePath, String fileType) {
		super.batchCreateClassFiles(baseModules, filePath, fileType);
	}

	/**
	 * Create xml header code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected void createXmlHeaderCode(StringBuilder sb) {
		//@off
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n")
		  .append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">").append("\n")
		  .append("\n");//@on
	}

	/**
	 * Create xml tag code.
	 * @param sb the sb
	 * @param mapperModule the base module
	 */
	protected void createXmlTagCode(StringBuilder sb, MapperModule mapperModule) {
		sb.append("<mapper namespace=\"").append(mapperModule.getMapperFullClassName()).append("\">").append("\n");
	}

	/**
	 * Create xml content.
	 * @param sb the sb
	 * @param mapperModule the base module
	 */
	protected abstract void createXmlContent(StringBuilder sb, MapperModule mapperModule);

	protected void createXmlEndTagCode(StringBuilder sb) {
		sb.append("\n").append("</mapper>");
	}

	@Override
	public String createClassCode(BaseModule baseModule) {
		return null;
	}
}
